package org.itstack.demo.design.impl;

import com.alibaba.fastjson.JSON;
import org.itstack.demo.design.HttpClient;
import org.itstack.demo.design.NetMall;
import sun.misc.BASE64Encoder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: fanYuLe
 * @CreateTime: 2023-12-29  10:54
 * @Description: 模拟JD商城
 * 模拟登录
 * 爬取信息，这⾥只是把 title 的信息爬取后的结果截取出来。
 * 模拟创建 base64 图⽚的⽅法
 */
public class JDNetMall extends NetMall {

    public JDNetMall(String uId, String uPwd) {
        super(uId, uPwd);
    }

    @Override
    protected Boolean login(String uId, String uPwd) {
        logger.info("模拟京东用户登录 uId：{} uPwd：{}",uId,uPwd);
        return true;
    }

    @Override
    protected Map<String, String> reptile(String skuUrl) {
        String str = HttpClient.doGet(skuUrl);
        Pattern p9 = Pattern.compile("(?<=title\\>).*(?=</title)");
        Matcher m9 = p9.matcher(str);
        Map<String,String> map =new ConcurrentHashMap<>();
        if(m9.find()){
            map.put("name",m9.group());
        }
        map.put("price","5999.00");
        logger.info("模拟京东商品爬虫解析：{} | {} 元 {}",map.get("name"),map.get("price"),skuUrl);
        return map;
    }

    @Override
    protected String createBase64(Map<String, String> goodsInfo) {
        BASE64Encoder encoder = new BASE64Encoder();
        logger.info("模拟生成京东商品base64海报");
        return encoder.encode(JSON.toJSONString(goodsInfo).getBytes());
    }
}
